package com.yuanfeng.base.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.zxing.NotFoundException;
import com.google.zxing.WriterException;
import com.yuanfeng.base.entity.BaseShareUrlEntity;
import com.yuanfeng.base.mapper.BaseShareUrlMapper;
import com.yuanfeng.base.service.BaseShareUrlService;
import com.yuanfeng.commoms.util.ParseTokenUtils;
import com.yuanfeng.commoms.util.ResponseResult;
import com.yuanfeng.commoms.util.secverifyapiutil.QRCodeUtil;
import com.yuanfeng.commoms.vo.user.UserInfoVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 *
 */
@Service
public class BaseShareUrlServiceImpl extends ServiceImpl<BaseShareUrlMapper, BaseShareUrlEntity>
    implements BaseShareUrlService{

    @Value("${domainName}")
    private String domainName;

    private static final Logger LOG = LoggerFactory.getLogger(BaseShareUrlServiceImpl.class);
    @Override
    public ResponseResult commonCreatQrCode(Map<String, Object> param) throws NotFoundException, IOException, WriterException {
        UserInfoVO userInfo = ParseTokenUtils.getUserInfoByToken(param.get("token").toString());
        //1、查询分享地址
        String shareUrl = this.baseMapper.selectOne(new LambdaQueryWrapper<BaseShareUrlEntity>()
                          .eq(BaseShareUrlEntity::getType,param.get("shareType").toString())).getShareUrl();
        if (StringUtils.isEmpty(shareUrl)) {
            return ResponseResult.fail(0, "分享地址未配置~");
        }

        shareUrl = domainName + shareUrl + "?shareUserId=" + userInfo.getUserId() + "&platForm=0" + "&shareType=" + param.get("shareType").toString() + "&relevanceId=" + userInfo.getShopId();

        String baseString = QRCodeUtil.text2Base64Str(shareUrl);

        Map<String, String> returnMap = new HashMap<>(8);
        returnMap.put("url", shareUrl);
        returnMap.put("baseString", baseString);

        LOG.info("分享路径：" + shareUrl);
        return ResponseResult.success(returnMap);
    }
}




